﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using TCP.SocketHelper;


namespace TCP.Test.Server
{
    public class Program
    {
        public static SocketServer<SocketClient> Server = new SocketServer<SocketClient>("*",8080,10);
        static void Main(string[] args)
        {
            if(args!=null && args.Length>0)
            {
                int len = args.Length;
                for(int i = 0;i<len;i++)
                {
                    switch(args[i])
                    {
                    }
                }
            }

            Server.LogAction += Log;
            Server.OnClientMsgReceiveAction += ReceiveData;
            Server.StartConnect();
            string cmd = "";
            do
            {
                cmd = Console.ReadLine();
                switch(cmd.ToLower())
                {
                    case "clear":
                        Console.Clear();
                        break;
                    case "send":
                        Console.Write("ClientName:");
                        string clientName = Console.ReadLine();
                        SocketClient client = Server[clientName];
                        if(client!=null)
                        {
                            Console.Write("Content:");
                            string content = Console.ReadLine();
                            client.SendMessage(Encoding.UTF8.GetBytes(content));
                        }
                        break;
                    case "list":
                        foreach(string clientkey in Server.ClientDict.Keys)
                        {
                            Console.WriteLine(clientkey);
                        }
                        break;
                }
            } while (cmd.ToLower() != "exit");
        }

        public static void Log(object log, int type)
        {
            Console.WriteLine(log);
        }

        public static void ReceiveData(byte[] data)
        {
            string str = Encoding.UTF8.GetString(data);
            Console.WriteLine("str:" + str);
        }
    }
}
